<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return['diy']=$this->plugin_setting('diy');
$basic=$this->plugin_setting('basic');
$return['basic']=$basic;
$return['banner']=$basic['image'];
if($_RQ['op']=='main'){
	$isfee=intval($_RQ['isfee']);
	//其实要展示的信息为课程信息
	if($_RQ['lessonid']){
		$lesson=pdo_get('jy_edu_lesson',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['lessonid']));
	}
	$classid=intval($_RQ['id']);
	if(!$classid){
		$classid=$lesson['classid'];
		if(!$classid){
			exi('没有找到该班级！','error');
		}
	}
	$class=pdo_get('jy_edu_class',array('uniacid'=>$_SESSION['uniacid'],'id'=>$classid));
	//查询本项目可服务员工(未锁定，状态为可服务)
	$worker_list=pdo_getall('jy_edu_worker',array('uniacid'=>$_SESSION['uniacid'],'status'=>'1','classid'=>$classid));
	//查询本项目的访问人数及添加访问人数
	/*
	$num=pdo_getcolumn('jy_edu_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']),'num');
	$serve['num']=$num+1;
	pdo_update('jy_edu_serve',array('num'=>$serve['num']),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	//已支付，已确认，已完成订单数
	$num1=pdo_count('jy_edu_order',array('uniacid'=>$_SESSION['uniacid'],'serve_id'=>$_RQ['id'],'status'=>'1'));
	$num2=pdo_count('jy_edu_order',array('uniacid'=>$_SESSION['uniacid'],'serve_id'=>$_RQ['id'],'status'=>'2'));
	$num3=pdo_count('jy_edu_order',array('uniacid'=>$_SESSION['uniacid'],'serve_id'=>$_RQ['id'],'status'=>'4'));
	$serve['ordernum']=$num1+$num2+$num3;
	$serve_time=json_decode($serve['serve_time'],true);
	*/
	//日期 今天，未来七天
	$data=array();
	$data['0']['riqi']=date("m-d",time("day")); 
	$data['1']['riqi']=date("m-d",strtotime("+1 day")); 
	$data['2']['riqi']=date("m-d",strtotime("+2 day")); 
	$data['3']['riqi']=date("m-d",strtotime("+3 day")); 
	$data['4']['riqi']=date("m-d",strtotime("+4 day")); 
	$data['5']['riqi']=date("m-d",strtotime("+5 day")); 
	$data['6']['riqi']=date("m-d",strtotime("+6 day")); 
	$data['0']['wi']=date("w"); 
	$data['1']['wi']=date("w",strtotime(date("Y-m-d",strtotime("+1 day")))); 
	$data['2']['wi']=date("w",strtotime(date("Y-m-d",strtotime("+2 day")))); 
	$data['3']['wi']=date("w",strtotime(date("Y-m-d",strtotime("+3 day")))); 
	$data['4']['wi']=date("w",strtotime(date("Y-m-d",strtotime("+4 day")))); 
	$data['5']['wi']=date("w",strtotime(date("Y-m-d",strtotime("+5 day")))); 
	$data['6']['wi']=date("w",strtotime(date("Y-m-d",strtotime("+6 day")))); 
	$weekarray=array("日","一","二","三","四","五","六"); //先定义一个数组
	$data['0']['xinq']= "周".$weekarray[$data['0']['wi']];
	$data['1']['xinq']= "周".$weekarray[$data['1']['wi']];
	$data['2']['xinq']= "周".$weekarray[$data['2']['wi']];
	$data['3']['xinq']= "周".$weekarray[$data['3']['wi']];
	$data['4']['xinq']= "周".$weekarray[$data['4']['wi']];
	$data['5']['xinq']= "周".$weekarray[$data['5']['wi']];
	$data['6']['xinq']= "周".$weekarray[$data['6']['wi']];
	
	//$this->session('serve_id',$_RQ['id']);
	
	//获取当前时间
	/*
	$time=time();
	foreach($serve_time as $k=>&$item){
		$num=pdo_count('jy_edu_order',array('timeid'=>$k,'uniacid'=>$_SESSION['uniacid'],'day'=>date("Y-m-d",time("day")),'serve_id'=>$_RQ['id']));
		
		if($item['yuyue']>$num){
		$item['yuyue']=$item['yuyue']-$num;
		}
		else{
		$item['yuyue']=0;
		}
		//时间转为时间戳
		$item['shijian']=strtotime(date("Y-m-d",time("day")).$item['times']['start']);
		if($item['shijian']<time()){
			$item['show']=0;
		}
		else{
			$item['show']=1;
		}
	}
	*/
	//查询当日课程
	$class_lesson=pdo_getcolumn('jy_edu_class',array('uniacid'=>$_SESSION['uniacid'],'id'=>$class['id']),'class_lesson');
	$class_lesson=json_decode($class_lesson,true);
	//查询当日课程
	if($class_lesson){
		$list=array();
		foreach($class_lesson as $item){
			if($item['xinqi']==$data['0']['xinq']){
				$list[]=$item;
			}
		}
		if($list){
			foreach($list as &$row){
				$row['worker']=pdo_get('jy_edu_worker',array('uniacid'=>$_SESSION['uniacid'],'classid'=>$class['id'],'lessonid'=>$row['lessonid']));
				$row['lesson']=pdo_get('jy_edu_lesson',array('uniacid'=>$_SESSION['uniacid'],'id'=>$row['lessonid']));
				$row['class']=pdo_get('jy_edu_class',array('uniacid'=>$_SESSION['uniacid'],'id'=>$row['lesson']['classid']));
				$row['classgroup']=pdo_get('jy_edu_class_group',array('uniacid'=>$_SESSION['uniacid'],'id'=>$row['class']['group_id']));
			//	$row['time']=json_decode($row['time'],true);
			}
		}
	}
	$return['class']=$class;
	$return['storetitle']=pdo_getcolumn('jy_edu_store',array('id'=>$class['storeid']),'title');
	$return['lessons']=$list;
	//$return['package']=json_decode($package);
	$return['worker_list']=$worker_list;
	$return['province']=file_get_contents('core/resource/area.txt');
	$return['province']=unserialize($return['province']);
	$return['data']=$data;
	$return['post']=array('lessonid'=>$_RQ['lessonid'],'bodyid'=>$_RQ['bodyid'],'classid'=>$class['id'],'storeid'=>$class['storeid'],'isfee'=>$isfee);
	exi($return);
}
if($_RQ['op']=='yuyue'){
	$postdata=get_postdata();
	//日期 今天，未来七天
	$data=array();
	$data['0']=date("Y-m-d",time("day")); 
	$data['1']=date("Y-m-d",strtotime("+1 day")); 
	$data['2']=date("Y-m-d",strtotime("+2 day")); 
	$data['3']=date("Y-m-d",strtotime("+3 day")); 
	$data['4']=date("Y-m-d",strtotime("+4 day")); 
	$data['5']=date("Y-m-d",strtotime("+5 day")); 
	$data['6']=date("Y-m-d",strtotime("+6 day"));
	$lessonid=intval($_RQ['lessonid']);
	//单课程,还未上课完的
	if($postdata['lessonid']){
		$iseduing=pdo_getcolumn('jy_edu_order',array('lesson_id'=>$postdata['lessonid'],'status'=>array(1,2),'ispay'=>1,'bodyid'=>$postdata['bodyid'],'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'id');
	}else{
		$iseduing=pdo_getcolumn('jy_edu_order',array('classid'=>$postdata['classid'],'status'=>array(1,2),'ispay'=>1,'bodyid'=>$postdata['bodyid'],'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'id');
	}
	if(!$iseduing){
		exi('','','jy_edu/create//storeid='.$postdata['storeid'].'&classid='.$postdata['classid'].'&lessonid='.$postdata['lessonid'].'&dayid='.$postdata['dayid'].'&isfee='.$postdata['isfee']);
	}
	$workerid=intval($_RQ['workerid']);
	if($postdata){
			//$time=ijson_encode(array('data'=>$postdata['data'],'start'=>$postdata['start'],'end'=>$postdata['end']));	
			$isllog=pdo_getcolumn('jy_edu_lesson_log',array('uniacid'=>$_SESSION['uniacid'],'lesson_id'=>$lessonid,'bodyid'=>$postdata['bodyid'],'day'=>$data[$postdata['dayid']]),'id');
			if($isllog){
				exi('该学生已经预约了当天的该课程！','error');
			}
			//查询当日课程		
			$insert=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$_SESSION['uid'],
				'bodyid'=>$postdata['bodyid'],
				'lesson_id'=>$lessonid,
				'classid'=>$postdata['classid'],
				'day'=>$data[$postdata['dayid']],
				'worker_id'=>$workerid,
				'remark'=>$postdata['remark'],
				'logsn'=>get_ordersn('jy_edu_lesson_log','logsn'),
				'ontime'=>ijson_encode(array('start'=>$_RQ['start'],'end'=>$_RQ['end'])),
				'createtime'=>TIMESTAMP,
				'storeid'=>$postdata['storeid']
			);
			pdo_insert('jy_edu_lesson_log',$insert);
			$id=pdo_insertid();
			exi('预约课程成功！','','jy_edu/mylesson_log/');
	}
	
}
if($_RQ['op']=='get_lesson'){
	if(!isset($_RQ['classid'])){
		exi('未获取门店信息','error');
	}
	if(!isset($_RQ['dayid'])){
		exi('请重新选择日期','error');
	}
	$class_lesson=pdo_getcolumn('jy_edu_class',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['classid']),'class_lesson');
	$class_lesson=json_decode($class_lesson,true);
	//查询当日课程
	if($class_lesson){
		$list=array();
		$wkey=$_RQ['wi'];
		foreach($class_lesson as $item){
			if($item['xinqi']==$wkey){
				$list[]=$item;
			}
		}
		if($list){
			
			foreach($list as &$row){
				$row['worker']=pdo_get('jy_edu_worker',array('uniacid'=>$_SESSION['uniacid'],'classid'=>$_RQ['classid'],'lessonid'=>$row['lessonid']));
				$row['lesson']=pdo_get('jy_edu_lesson',array('uniacid'=>$_SESSION['uniacid'],'id'=>$row['lessonid']));
				$row['class']=pdo_get('jy_edu_class',array('uniacid'=>$_SESSION['uniacid'],'id'=>$row['lesson']['classid']));
				$row['classgroup']=pdo_get('jy_edu_class_group',array('uniacid'=>$_SESSION['uniacid'],'id'=>$row['class']['group_id']));
			}
		}
	}
	$return['list']=$list;
	$return['success']=1;
	
	exi($return);
}
if($_RQ['op']=='package'){
	if(!isset($_RQ['id'])){
		exi('请重新选择项目','error');
	}
	//exi($_RQ['id'],'error');
	$package=pdo_getcolumn('jy_edu_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']),'package');
	$package=json_decode($package,true);
	//查询自定义字段
	$fileds=pdo_getall('jy_edu_fileds',array('uniacid'=>$_SESSION['uniacid'],'serve_id'=>$_RQ['id']));
	foreach($fileds as &$item){
		$item['value']=explode(';',$item['value']);
	}
	$return['post']=array();
	$return['post']['fileds']=$fileds;
	$return['package']=$package;
	$return['success']=1;
	exi($return);
}